#ifndef THINK_THINK_CONSOLE_OUTPUT_QUESTION_CHOICE_H
#define THINK_THINK_CONSOLE_OUTPUT_QUESTION_CHOICE_H

static char* think_console_output_question_choice =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: yunwuxin <448901948@qq.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\console\\output\\question;\n"
	"\n"
	"use think\\console\\output\\Question;\n"
	"\n"
	"class Choice extends Question\n"
	"{\n"
	"\n"
	"    private $choices;\n"
	"    private $multiselect  = false;\n"
	"    private $prompt       = \' > \';\n"
	"    private $errorMessage = \'Value \"%s\" is invalid\';\n"
	"\n"
	"    /**\n"
	"     * 构造方法\n"
	"     * @param string $question 问题\n"
	"     * @param array  $choices  选项\n"
	"     * @param mixed  $default  默认答案\n"
	"     */\n"
	"    public function __construct($question, array $choices, $default = null)\n"
	"    {\n"
	"        parent::__construct($question, $default);\n"
	"\n"
	"        $this->choices = $choices;\n"
	"        $this->setValidator($this->getDefaultValidator());\n"
	"        $this->setAutocompleterValues($choices);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 可选项\n"
	"     * @return array\n"
	"     */\n"
	"    public function getChoices()\n"
	"    {\n"
	"        return $this->choices;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置可否多选\n"
	"     * @param bool $multiselect\n"
	"     * @return self\n"
	"     */\n"
	"    public function setMultiselect($multiselect)\n"
	"    {\n"
	"        $this->multiselect = $multiselect;\n"
	"        $this->setValidator($this->getDefaultValidator());\n"
	"\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    public function isMultiselect()\n"
	"    {\n"
	"        return $this->multiselect;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取提示\n"
	"     * @return string\n"
	"     */\n"
	"    public function getPrompt()\n"
	"    {\n"
	"        return $this->prompt;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置提示\n"
	"     * @param string $prompt\n"
	"     * @return self\n"
	"     */\n"
	"    public function setPrompt($prompt)\n"
	"    {\n"
	"        $this->prompt = $prompt;\n"
	"\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置错误提示信息\n"
	"     * @param string $errorMessage\n"
	"     * @return self\n"
	"     */\n"
	"    public function setErrorMessage($errorMessage)\n"
	"    {\n"
	"        $this->errorMessage = $errorMessage;\n"
	"        $this->setValidator($this->getDefaultValidator());\n"
	"\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取默认的验证方法\n"
	"     * @return callable\n"
	"     */\n"
	"    private function getDefaultValidator()\n"
	"    {\n"
	"        $choices      = $this->choices;\n"
	"        $errorMessage = $this->errorMessage;\n"
	"        $multiselect  = $this->multiselect;\n"
	"        $isAssoc      = $this->isAssoc($choices);\n"
	"\n"
	"        return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {\n"
	"            // Collapse all spaces.\n"
	"            $selectedChoices = str_replace(\' \', \'\', $selected);\n"
	"\n"
	"            if ($multiselect) {\n"
	"                // Check for a separated comma values\n"
	"                if (!preg_match(\'/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/\', $selectedChoices, $matches)) {\n"
	"                    throw new \\InvalidArgumentException(sprintf($errorMessage, $selected));\n"
	"                }\n"
	"                $selectedChoices = explode(\',\', $selectedChoices);\n"
	"            } else {\n"
	"                $selectedChoices = [$selected];\n"
	"            }\n"
	"\n"
	"            $multiselectChoices = [];\n"
	"            foreach ($selectedChoices as $value) {\n"
	"                $results = [];\n"
	"                foreach ($choices as $key => $choice) {\n"
	"                    if ($choice === $value) {\n"
	"                        $results[] = $key;\n"
	"                    }\n"
	"                }\n"
	"\n"
	"                if (count($results) > 1) {\n"
	"                    throw new \\InvalidArgumentException(sprintf(\'The provided answer is ambiguous. Value should be one of %s.\', implode(\' or \', $results)));\n"
	"                }\n"
	"\n"
	"                $result = array_search($value, $choices);\n"
	"\n"
	"                if (!$isAssoc) {\n"
	"                    if (!empty($result)) {\n"
	"                        $result = $choices[$result];\n"
	"                    } elseif (isset($choices[$value])) {\n"
	"                        $result = $choices[$value];\n"
	"                    }\n"
	"                } elseif (empty($result) && array_key_exists($value, $choices)) {\n"
	"                    $result = $value;\n"
	"                }\n"
	"\n"
	"                if (empty($result)) {\n"
	"                    throw new \\InvalidArgumentException(sprintf($errorMessage, $value));\n"
	"                }\n"
	"                array_push($multiselectChoices, $result);\n"
	"            }\n"
	"\n"
	"            if ($multiselect) {\n"
	"                return $multiselectChoices;\n"
	"            }\n"
	"\n"
	"            return current($multiselectChoices);\n"
	"        };\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif